package org.jfinanceobserver.persistence.dbms;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jfinanceobserver.persistence.ConnFactory;
import org.jfinanceobserver.tmp.App;

/**
 * This class handled the directs connection to the MySQL DBMS
 *
 * $Author: r78v10a07@gmail.com $ $LastChangedDate: 2013-02-11 13:03:43 +0000 (Mon, 11 Feb 2013) $ $LastChangedRevision: 2 $
 *
 * @since Feb 10, 2013
 */
public class MySQLConnect {

    private Connection conn = null;

    /**
     * Get the Connection
     *
     * @return the Connection
     */
    public Connection getConn() {
        return conn;
    }

    /**
     * Close the connection
     *
     * @throws SQLException
     */
    public void close() throws SQLException {
        if (conn != null) {
            if (!conn.isClosed()) {
                conn.close();
            }
        }
    }

    /**
     * True if the connection is closed
     *
     * @return True if the connection is closed
     * @throws SQLException
     */
    public boolean isClosed() throws SQLException {
        if (conn != null) {
            if (conn.isClosed()) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * Executes the given SQL statement, which may be an INSERT, UPDATE, or
     * DELETE statement or an SQL statement that returns nothing, such as an SQL
     * DDL statement
     *
     * @param sql an SQL Data Manipulation Language (DML) statement, such as
     * INSERT, UPDATE or DELETE; or an SQL statement that returns nothing, such
     * as a DDL statement
     * @return either (1) the row count for SQL Data Manipulation Language (DML)
     * statements or (2) 0 for SQL statements that return nothing
     * @throws SQLException
     */
    public int executeUpdate(String sql) throws SQLException {
        int count;
        try (Statement s = conn.createStatement()) {
            count = s.executeUpdate(sql);
        }
        return count;
    }

    /**
     * Open the MySQL connection
     */
    public void open() {
        try {
            try {
                Class.forName(ConnFactory.getInstance().getDriver()).newInstance();
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
            }

            java.util.Properties conProperties = new java.util.Properties();
            conProperties.put("user", ConnFactory.getInstance().getUser());
            conProperties.put("password", ConnFactory.getInstance().getPasswd());
            conProperties.put("autoReconnect", "true");
            conProperties.put("maxReconnects", "10");
            conn = DriverManager.getConnection(ConnFactory.getInstance().getUrl(), conProperties);
        } catch (SQLException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
